package com.fone.player.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.fone.player.R;
import com.fone.player.activity.personal.UserInfoActivity;
import com.fone.player.client.Callback;
import com.fone.player.client.Error;
import com.fone.player.client.LoginspaceRst;
import com.fone.player.client.Request;
import com.fone.player.sns.UserInfoManager;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.util.ScreenUtil;

public class LoginoutDialog implements OnClickListener {
	
	private static final String TAG = "LoginoutDialog";
	
	private UserInfoActivity mActivity;
	private CustomDialog dialog;
	
	public LoginoutDialog(Context mContext, UserInfoActivity activity) {
		mActivity = activity; 
		
		View changeNameView = LayoutInflater.from(mContext).inflate(
				R.layout.loginout_alert_dialog, null);
		dialog = new CustomDialog((Context) activity,
				changeNameView);
		dialog.setCanceledOnTouchOutside(false);

		dialog.show();

		changeNameView.setMinimumWidth((int) (ScreenUtil
				.getScreenWidthPix(mContext) * 0.8));// 设置dialog的宽度

		Button changeSureBtn = (Button) changeNameView
				.findViewById(R.id.loginout_sure_id);
		changeSureBtn.setOnClickListener(this);

		Button changeCancelBtn = (Button) changeNameView
				.findViewById(R.id.loginout_cancel_id);
		changeCancelBtn.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.loginout_sure_id:
			mActivity.mHandler.sendEmptyMessage(UserInfoActivity.SHOW_WAITTING);
			new Thread(){
				public void run() {
					UserInfoManager.exitLogin();
					mActivity.mHandler.sendEmptyMessage(UserInfoActivity.LOGINOUT_SUCCESS);
				};
			}.start();
			dialog.cancel();
			if(FoneUtil.isNetOkWithToast(mActivity.getApplicationContext())){
				Request.getInstance().loginspace(-1,0,0,null, new loginOutCallBack());
			}
			break;
		case R.id.loginout_cancel_id:
			dialog.cancel();
			break;

		default:
			break;
		}
	}
	
	/**
	 * 注销回调
	 * @author user
	 *
	 */
	private class loginOutCallBack implements Callback<LoginspaceRst>{

		@Override
		public void onSuccess(LoginspaceRst t) {
			L.v(TAG, "loginOutCallBack onSuccess", t.toString());
			if(t.result!=0){
				if(t.error!=null){
//					FoneUtil.showToast(mActivity.getApplicationContext(), "注销失败");
//					mActivity.mHandler.sendEmptyMessage(UserInfoActivity.SHOW_WAITTING);
					return;
				}
			}
		}

		@Override
		public void onFailure(Error error) {
//			mActivity.mHandler.sendEmptyMessage(UserInfoActivity.CANCEL_WAITTING);
			L.v(TAG,
					"loginOutCallBack onFailure",
					"isNetworkError : " + error.isNetworkError() + " status : "
							+ error.getStatus() + " reason : "
							+ error.getReason());
			
//			if(error.isNetworkError()){
//				FoneUtil.showToast(mActivity.getApplicationContext(), "网络超时,请稍后重试");
//				return ;
//			}
//			FoneUtil.showToast(mActivity.getApplicationContext(), "注销失败");
		}
	}
	
}
